package com.xzl.xep.common.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import org.apache.commons.lang3.StringUtils;

/**
 * 
 * 
 * Title:
 * 
 * Description:
 * 
 * Copyright: Copyright (c) 2008
 * 
 * @author haoxz11
 * @created Mar 7, 2008 1:38:18 PM
 * @version $Id: SecurityUtil.java,v 1.1 2013/10/23 03:26:18 haoxz11 Exp $
 */
public class SecurityUtil {

	public static String encrypt(int istr) {
		return encrypt(istr + "");
	}
	
	public static String encrypt(long istr) {
		return encrypt(istr + "");
	}

	/**
	 * 加密
	 */
	public static String encrypt(String str) {
		// System.err.println("encrypt====" + str);
		String re = null;
		if (!StringUtils.isEmpty(str)) {
			try {
				InputStream inputstream = new ByteArrayInputStream(str.getBytes());
				ByteArrayOutputStream sout = new ByteArrayOutputStream();
				BlockCipherUtil.encrypt(inputstream, sout);
				re = sout.toString();
			} catch (RuntimeException e) {
				e.printStackTrace();
			}
		}
		return re;
	}
	
	public static int decryptToInt(String str) {
		return NumberUtil.parseInt(decrypt(str));
	}

	/**
	 * 解密
	 * 
	 * @param str
	 * @return
	 */
	public static String decrypt(String str) {
//		 System.err.println("decrypt====" + str);
		String re = null;
		if (!StringUtils.isEmpty(str)) {
			try {
				InputStream inputstream = new ByteArrayInputStream(str.getBytes());
				ByteArrayOutputStream sout = new ByteArrayOutputStream();
				BlockCipherUtil.decrypt(inputstream, sout);
				re = sout.toString();
//				 System.err.println("re====" + re);
			} catch (Exception e) {
//				e.printStackTrace();
			}
		}
		return re;

	}
	
	public static void main(String[] args){
		System.out.println(SecurityUtil.decrypt(args[0]));
	}
}
